﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerXMLWPF
{
    /// <summary>
    /// Interaction logic for EditWater.xaml
    /// </summary>
    public partial class EditWater : Window
    {
        public EditWater()
        {
            InitializeComponent();
            LoadContent();
        }

        private void LoadContent()
        {
            if (Global.water == null)
                Global.water = new Water();
            txtName.Text = Global.water.Name;
            txtAmount.Text = Global.water.Amount;
            txtCalcium.Text = Global.water.Calcium;
            txtBicarbonate.Text = Global.water.Bicarbonate;
            txtChloride.Text = Global.water.Chloride;
            txtMagnesium.Text = Global.water.Magiesium;
            txtSoldium.Text = Global.water.Sodium;
            txtSulfate.Text = Global.water.Sulfate;
            txtPH.Text = Global.water.PH;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter Water name");
                return;
            }
            try
            {
                double amount = double.Parse(txtAmount.Text);
                if (amount <= 0)
                {
                    MessageBox.Show("Amount must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Amount must be a double and greater than 0");
                return;
            }
            try
            {
                double amount = double.Parse(txtCalcium.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Calcium must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Calcium must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtMagnesium.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Magnesium must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Magnesium must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtBicarbonate.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Bicarbonate must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Bicarbonate must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtSulfate.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Sulfate must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Sulfate must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtChloride.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Chloride must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Chloride must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtSoldium.Text);
                if (amount <= 0 || amount >= 10000)
                {
                    MessageBox.Show("Sodium must be a double and in bettween 0 and 1000000");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Sodium must be a double and in bettween 0 and 1000000");
                return;
            }
            try
            {
                double amount = double.Parse(txtPH.Text);
                if (amount <= 0 || amount > 14)
                {
                    MessageBox.Show("PH must be a double and in bettween 0 and 14");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("PH must be a double and in bettween 0 and 14");
                return;
            }
            Global.water.Name = txtName.Text;
            Global.water.Calcium = double.Parse(txtCalcium.Text).ToString();
            Global.water.Amount = double.Parse(txtAmount.Text).ToString();
            Global.water.Display_Amount = Global.hop.Amount + " l";
            Global.water.Bicarbonate = double.Parse(txtBicarbonate.Text).ToString();
            Global.water.Sulfate = double.Parse(txtSulfate.Text).ToString();
            Global.water.Chloride = double.Parse(txtChloride.Text).ToString();
            Global.water.Sodium = double.Parse(txtSoldium.Text).ToString();
            Global.water.Magiesium = double.Parse(txtMagnesium.Text).ToString();
            Global.water.PH = double.Parse(txtPH.Text).ToString();
            Global.water.Version = "1";
            Global.OK = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
